package com.bizihang.security.core.validate.code;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 毕子航 951755883@qq.com
 * @date 2018/08/18
 */
@RestController
public class ValidateCodeController {
	@Autowired
	Map<String, ValidateCodeProcessor> validateCodeProcessorMap;

	/**
	 * 创建验证码
	 *
	 * @param type     {@link ValidateCodeType}
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@GetMapping("/code/{type}")
	public void createImageCode(@PathVariable String type, HttpServletRequest request, HttpServletResponse response) throws Exception {
		validateCodeProcessorMap.forEach((k, validateCodeProcessor) -> {
			if (k.startsWith(type.toLowerCase())) {
				try {
					validateCodeProcessor.create(new ServletWebRequest(request, response));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
